package com.it.base.arithmetic.linkend;

public class DubleLinkedListDemo {
    public static void main(String[] args) {
        System.out.println("双向链表的测试");
        HeroNode2 hero1 =  new HeroNode2(1,"宋江","及时雨");
        HeroNode2 hero2 =  new HeroNode2(2,"卢俊义","玉麒麟");
        HeroNode2 hero3 =  new HeroNode2(3,"吴用","智多星");
        HeroNode2 hero4 =  new HeroNode2(4,"林冲","豹子头");
        HeroNode2 hero5 =  new HeroNode2(5,"张三","自定义");
        DubleLinkedList dubleLinkedList = new DubleLinkedList();
        dubleLinkedList.addNode(hero1);
        dubleLinkedList.addNode(hero2);
        dubleLinkedList.addNode(hero3);

        dubleLinkedList.addNode(hero4);

        dubleLinkedList.addNode(hero5);
        dubleLinkedList.showList();
        //修改
        HeroNode2 heroNode6 =new HeroNode2(5,"李四","修改的叫李四");
        dubleLinkedList.update(heroNode6);
        dubleLinkedList.showList();
        //删除
        dubleLinkedList.delHearNode(1);
        dubleLinkedList.delHearNode(2);
        System.out.println("删除后为");
        dubleLinkedList.showList();
        System.out.println("测试按照编号添加");
        HeroNode2 hero10 =  new HeroNode2(10,"宋江","及时雨");
        HeroNode2 hero12 =  new HeroNode2(12,"卢俊义","玉麒麟");
        HeroNode2 hero13 =  new HeroNode2(13,"吴用","智多星");
        HeroNode2 hero14 =  new HeroNode2(14,"林冲","豹子头");
        HeroNode2 hero15 =  new HeroNode2(15,"张三","自定义");
        DubleLinkedList dubleLinkedListByNo = new DubleLinkedList();
       /* dubleLinkedListByNo.addNodeByNo(hero15);
        dubleLinkedListByNo.addNodeByNo(hero10);
        dubleLinkedListByNo.addNodeByNo(hero14);
        dubleLinkedListByNo.addNodeByNo(hero13);
        dubleLinkedListByNo.addNodeByNo(hero12);*/
        dubleLinkedListByNo.showList();
    }
}
class DubleLinkedList{
    private HeroNode2 head=new HeroNode2(0,"","");
    //返回头节点
    public HeroNode2 getHead(){
        return head;
    }

    //遍历双向链表
    public void showList(){
        //判断链表是否为空
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        //遍历链表
        HeroNode2 temp = head.next;
        while (true){
            //找到链表的最后一个
            if(temp == null){
                break;
            }
            System.out.println(temp);
            //一定要后移，不然死循环
            temp=temp.next;

        }

    }
    //添加节点到单向链表,不排序
    public void addNode(HeroNode2 heroNode){
        HeroNode2 temp = head;
        //遍历链表
        while (true){
            //找到链表的最后一个
            if(temp.next == null){
                break;
            }
            //如果没有找到，就后移
            temp=temp.next;
        }
        //退出循环，temp指向链表的最后一个节点,
        //双向指定，形成双向链表
        temp.next=heroNode;
        heroNode.pre=temp;
    }
    //添加节点到双向链表,按照编号排序添加
   /* public void addNodeByNo(HeroNode2 heroNode){

        HeroNode2 temp = head;
        boolean flag= false;
        //遍历链表
        while (true){
            //找到链表的最后一个
            if(temp.next == null){
                break;
            }
            //查找需要添加的位置
            if(temp.next.no >heroNode.no){

                break;
            }
            if(temp.next.no ==heroNode.no){
                System.out.println("数据已存在");
                break;
            }
            //如果没有找到，就后移
            temp=temp.next;
        }

        if(flag){

            System.out.println("数据已存在");
        }
        //找到位置了
        else{

            heroNode.pre=temp;
            if(temp.next!=null){
                temp.next.pre=heroNode;
                temp.next=heroNode;
                heroNode.next=temp.next.next;
            }
            temp.pre.next=heroNode;
            temp.pre=heroNode;
            heroNode.pre=temp.pre;
            heroNode.next=temp;

        }

    }*/
    //修改节点的内容
    //根据编号no来修改，no编号不能改变
    public void update(HeroNode2 heroNode){
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true){
            //找到最后
            if(temp==null){
                break;
            }
            //找到了
            if(temp.no== heroNode.no){
                flag=true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            temp.name=heroNode.name;
            temp.nickname=heroNode.nickname;
        }
        else{
            System.out.println("没有找到no对应的节点");
        }
    }
    //从双向链表中删除一个节点
    //根据编号来删除
    //我们可以直接找到删除节点，找到之后，自我删除即可
    public void delHearNode(int no){
        //判断链表是否为空
        if(head.next==null){

            System.out.println("链表为空，无法删除");
            return;
        }
        //从第一个有效节点开始查找
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true){
            if(temp==null){
                break;
            }
            //找到要删除节点的前一个节点
            if(temp.no ==no){
                flag=true;
                break;
            }
            temp = temp.next;

        }
        if(flag){
            temp.pre.next=temp.next;
            //如果temp是最后一个节点的情况，那么temp.next是空，回出现空指针异常
            if(temp.next!=null){
                temp.next.pre=temp.pre;

            }
        }else {
            System.out.println("无法删除，没有找到要删除的节点");
        }
    }
}
//创建一个双向链表的类
class HeroNode2{
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;//指向下一个节点的
    public HeroNode2 pre;//指向上一个节点
    public HeroNode2(int no,String name,String nickname){
        this.no=no;
        this.name=name;
        this.nickname=nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
